package com.hgb.mall.admin.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.admin.services.SmsHomeAdvertiseService;
import com.hgb.mall.mbg.entity.SmsHomeAdvertise;
import com.hgb.mall.mbg.mapper.SmsHomeAdvertiseDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.SmsHomeAdvertiseMapper;
import lombok.var;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 20:38 2021/1/11
 * @Modified By:
 */
@Service
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {
    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    @Override
    public int create(SmsHomeAdvertise advertise) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        smsHomeAdvertiseMapper.insert(advertise);
        return smsHomeAdvertiseMapper.insert(advertise);
    }

    @Override
    public int delete(List<Long> ids) {
        return smsHomeAdvertiseMapper.delete(c -> c.where(SmsHomeAdvertiseDynamicSqlSupport.id, isInWhenPresent(ids)));
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        SmsHomeAdvertise record = new SmsHomeAdvertise();
        record.setId(id);
        record.setStatus(status);
        return smsHomeAdvertiseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public SmsHomeAdvertise getItem(Long id) {
        return smsHomeAdvertiseMapper.selectByPrimaryKey(id).orElse(null);
    }

    @Override
    public int update(Long id, SmsHomeAdvertise advertise) {
        advertise.setId(id);
        return smsHomeAdvertiseMapper.updateByPrimaryKeySelective(advertise);
    }

    @Override
    public List<SmsHomeAdvertise> list(String name, Integer type, String endTime, Integer pageSize, Integer pageNum) {

        var builder = select(SmsHomeAdvertiseMapper.selectList)
                .from(SmsHomeAdvertiseDynamicSqlSupport.smsHomeAdvertise)
                .where(SmsHomeAdvertiseDynamicSqlSupport.name, isLikeWhenPresent(name)
                        .then(s -> "%" + s + "%"))
                .and(SmsHomeAdvertiseDynamicSqlSupport.type, isEqualToWhenPresent(type));
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.hasLength(endTime)) {
            String startStr = endTime + " 00:00:00";
            String endStr = endTime + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                builder.and(SmsHomeAdvertiseDynamicSqlSupport.endTime, isBetween(start).and(end));
            }
        }
        return smsHomeAdvertiseMapper.selectMany(builder.build().render(RenderingStrategies.MYBATIS3));
    }
}
